<?php

namespace App\Domain\ChannelContact\Services;

use App\Domain\ChannelContact\Dao\ChannelContactDao;
use App\Domain\ChannelContact\Models\ChannelContactModel;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;
use App\Exceptions\BadRequestException;

class ChannelContactService
{
    function contractId()
    {
        $channelcontractDao = new ChannelContactDao();
        $data = $channelcontractDao->findNew();
        $no = $data['contact_id'];
        //原来的年
        $no1 = substr($no, 0, 4);
        //原来的编号
        $no2 = substr($no, -5);
        //现在的年
        $now = date("Y");
        //新合同
        if ($now == $no1) {

            $new_no2 = $no2 + 1;
            $new_no2 = sprintf("%05d", $new_no2);
            $new = date("Ymdh") . $new_no2;
        } else {
            $new = date("Ymdh") . "00001";
        }
        return $new;
    }

    function create($param)
    {
        $channelcontractDao = new ChannelContactDao();
        $data = $channelcontractDao->findByName($param['name']);
        if ($param['name'] === $data['name']) {
            throw new BadRequestException('合同已存在');
        }
        $res = $channelcontractDao->create([
            'contact_id' => $this->contractId(),
            'name' => $param['name'],
            'channel_name' => $param['channel_name'],
            'channel_id' => $param['channel_id'],
            'start_time' => $param['start_time'],
            'end_time' => $param['end_time'],
            'enclosure' => $param['enclosure'],
            'signatory' => $param['signatory'],
            'status' => $param['status'],
        ]);
        return $res;
    }

    function update($param)
    {
        $channelcontractDao = new ChannelContactDao();
        $data = $channelcontractDao->findByContactId($param['contact_id']);
        if (empty($data)) {
            throw new BadRequestException('合同不存在');
        }
        $res = [
            'name' => $param['name'],
            'channel_name' => $param['channel_name'],
            'channel_id' => $param['channel_id'],
            'start_time' => $param['start_time'],
            'end_time' => $param['end_time'],
            'enclosure' => $param['enclosure'],
            'signatory' => $param['signatory'],
            'status' => $param['status'],
        ];
        $re = $channelcontractDao->update($data, $res);
        return $re;
    }

    public function del($id)
    {
        $channelcontractDao = new ChannelContactDao();
        $data = $channelcontractDao->del($id);
        return $data;
    }

    /**
     * 合同列表
     * @param $params
     * @return LengthAwarePaginator|Builder[]|Collection
     */
    public function list($params)
    {
        $query = ChannelContactModel::query()
            ->select('channel_contract.*')
            ->where('channel_contract.state', 1);
        $query = $query->where(function (Builder $query) use ($params) {
            if (isset($params['contact_id']))
                $query->where('contact_id', 'like', "%{$params['contact_id']}%");
            if (isset($params['channel_name']))
                $query->where('channel_name', 'like', "%{$params['channel_name']}%");
            if (isset($params['start_time']) && !empty($params['start_time']))
                $query->where('start_time', '>=', $params['start_time']);
            if (isset($params['end_time']) && !empty($params['end_time']))
                $query->where('end_time', '<=', $params['end_time']);
            if (isset($params['status']) && !empty($params['status']))
                $query->where('status', $params['status']);
        });
        if (isset($params['pageSize']) && isset($params['currentPage'])) {
            return $query->paginate($params['pageSize'], ['*'], 'page', $params['currentPage']);
        } else {
            return $query->get();
        }
    }


    public function info($id)
    {
        $channelcontractDao = new ChannelContactDao();
        $data = $channelcontractDao->findById($id);
        return $data;
    }

    public function findByContactId($contact_id)
    {
        $channelcontractDao = new ChannelContactDao();
        $data = $channelcontractDao->findByContactId($contact_id);
        return $data;
    }
}
